/*
* @Author: sam
* @Date:   2017-10-11 14:06:14
* @Last Modified by:   sam
* @Last Modified time: 2017-10-25 10:32:13
*/
(function(win){
	var Map = function(id){
		this.init.call(this,id);
	};
	Map.prototype = {
		init : function(id){//初始化
			var that = this;
			this.prefix = '_m';
			this.stores = {};
			this.canClick = true;
			this.activityCode = '';
			this.activityOverlays = [];
			this.activitySendCode = {
				'shi' : '',
				'xian' : '',
				'zhen' : '',
				'cun' : '',
				'one' : ''
			};
			this.zoomlist = {};
			this.map = new AMap.Map(id, {
				dragEnable : true,
				center: [112.938653,27.833165],
		        zoom:8
		    });
		    this.toolBar = new AMap.ToolBar({
		        position: "RT"
		    });
		    this.map.addControl(this.toolBar);
			return this;
		},
		setZoonAndCenterByType : function(type,obj){
			var zoomData = this.zoomlist[type];
			if(obj === undefined){//设置已有值
				if(zoomData !== null){
					this.setZoomAndCenter(zoomData);
				}
			}else{//赋值并设置
				this.zoomlist[type] = obj;
				this.setZoomAndCenter(this.zoomlist[type]);
			}
		},
		setZoomAndCenter : function(obj){//设置中心
			//lng,lat,zoom
			if(obj !== undefined){
				this.map.setZoomAndCenter(obj.zoom,new AMap.LngLat(obj.lng,obj.lat));
			}
		},
		drawPolygon : function(polygon){
			//lng,lat,zoom,key,data,fn
			var that = this;
			var _points = [],_polygon,polygons,tempArr = [],_marker;
			var store = this.stores[this.prefix + polygon.code];
			var color = polygon.color;
			//console.log(this);

			if(store === undefined){
				this.stores[this.prefix + polygon.code] = {};
				this.stores[this.prefix + polygon.code]['data'] = polygon;
			}
			// this.setZoomAndCenter.call(this,{
			// 	lng : polygon.lng,
			// 	lat : polygon.lat,
			// 	zoom : polygon.zoom
			// });

			if(polygon.lng !== null){
				this.drawMarker.call(this,{
					lng : polygon.lng,
					lat : polygon.lat,
					name : polygon.name,
					code : polygon.code
				});
			}

			polygons = this.stores[this.prefix + polygon.code]['polygons'];

			if(polygons === undefined){
				console.log('新建区域');
				this.stores[this.prefix + polygon.code]['polygons'] = [];
				polygons = this.stores[this.prefix + polygon.code]['polygons'];
				makeTempArr(JSON.parse(polygon.data.trim()));
				for(var i in tempArr){
					(function(i){
						_points = [];
						filtData(tempArr[i]);
						//坐标转换
					    _polygon = new AMap.Polygon({
					        path: _points,//设置多边形边界路径
					        strokeColor: color, //线颜色 "#1ec4ff"
					        strokeOpacity: 1, //线透明度
					        strokeWeight: 2,    //线宽
					        fillColor: color, //填充色 "#cde2e7"
					        fillOpacity: 0.7//填充透明度
					    });
					    _polygon.on('click',function(e){
					    	if(that.canClick){
								//设置活动对象
								//if(that.activityCode !== polygon.code){
									that.setActivity(polygon.code);
							    	polygon.fnclick(that,polygon,e);
								//}
					    	}
					    });
					    polygons.push(_polygon);
					    _polygon.setMap(that.map);
					})(i);
				}
			}else{
				for(var i=0; i< polygons.length; i++){
					polygons[i].show();
				}
			}

			//收集所有坐标点
			function addPoint(arr){
				_points.push(new AMap.LngLat(arr[0],arr[1]));
			};

			//遍历数据
			function filtData(data){
				for(var i = 0;i<data.length;i++){
					if(typeof data[i] !== 'object'){
						addPoint(data);
					}else{
						filtData(data[i]);
					}
				}
			};

			//创建临时数组
			function makeTempArr(data){
			    for(var i in data){
			     	if(Array.isArray(data[i])){
						tempArr[i] = data[i];
					}
			    }
			};

			//随机产生色值
			function randomColor(){
              	var colorValue="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
              	var colorArray = colorValue.split(",");
               	var color="#";//定义一个存放十六进制颜色值的字符串变量，先将#存放进去
               	for(var i=0;i<6;i++){
                   	color+=colorArray[Math.floor(Math.random()*16)];
               	}
               	console.log(color);
               	return color;
			}
		},
		drawMarker : function(marker){
			var _marker;
			_marker = this.stores[this.prefix + marker.code]['markers'];
			if(_marker === undefined){
				_marker = new AMap.Marker({
					position : new AMap.LngLat(marker.lng,marker.lat)
				});
				_marker.setLabel({
					offset: new AMap.Pixel(-10, -25),
					content : marker.name
				});
				this.stores[this.prefix + marker.code]['markers'] = [];
				this.stores[this.prefix + marker.code]['markers'].push(_marker);
				_marker.setMap(this.map);
			}else{
				for(var i=0; i<_marker.length; i++){
					_marker[i].show();
				}
			}
		},
		mapClick : function(fn){
			var that = this;
			this.map.on('click',function(){
				if(that.canClick){
					fn(that);
				}
			});
		},
		changeClickStatus : function(boolean){
			this.canClick = boolean;
		},
		setActivity : function(code){
			var _markers,_polygons;
			// if(this.activityCode !== ''){
			// 	_markers = this.stores[this.prefix + this.activityCode]['markers'];
			// 	_polygons = this.stores[this.prefix + this.activityCode]['polygons'];
			// 	for(var i = 0;i<_markers.length;i++){
			// 		_markers[i].hide();
			// 	}
			// 	for(var j = 0;j<_polygons.length;j++){
			// 		_polygons[j].hide();
			// 	}
			// }
			this.activityCode = code;
		},
		setOverlays : function(code){
			this.activityOverlays.push(code);
		},
		hideOverlays : function(){
			var markers,polygons,code;
			while(this.activityOverlays.length !== 0){
				code = this.activityOverlays.shift();
				markers = this.stores[this.prefix + code]['markers'];
				if(markers && markers.length === 1){
					markers[0].hide();
				}
				polygons = this.stores[this.prefix + code]['polygons'];
				for(var i = 0; i < polygons.length; i++){
					polygons[i].hide();
				}
			}
		},
		saveActivitySendCode : function(status,code){
			this.activitySendCode[status] = code;
		}
	};
	win.Class = win.Class || {};
	win.Class.Map = Map;
})(window);